﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Sys.Orders
{
    public partial class Print2 : InfoPage
    {
        BLL.Orders bll = new BLL.Orders();
        Model.Orders model = new Model.Orders();
        //
        BLL.V_Orders bllV_Orders = new BLL.V_Orders();
        //
        BLL.V_Orders_Goods bllV_Orders_Goods = new BLL.V_Orders_Goods();
        BLL.Orders_Goods bllOrders_Goods = new BLL.Orders_Goods();
        //
        BLL.Store bllStore = new BLL.Store();
        Model.Store modelStore = new Model.Store();
        //
        BLL.Orders_State_Log bllOrders_State_Log = new BLL.Orders_State_Log();
        //
        List<Model.V_Orders_Goods> listModelV_Orders_Goods = new List<Model.V_Orders_Goods>();
        //
        protected decimal ExpectPriceSum = 0, ActualPriceSum_Buying = 0;
        EnumHelper.OrdersState OrderState;
        bool BeforeAndBuying = false, AfterAndBuying = false;
        protected string IsDelivery = "";
        int prevSupplierID = 0;
        decimal thisSupplier_ExpectBuyPriceSum = 0, thisSupplier_ActualBuyPriceSum = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            IsDelivery = DataTypePlus.requestString(Request.QueryString["IsDelivery"]);
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {


        }
        public override void Page_Load_More()
        {
            if (Master.idStr != "")
            {
                string sql = "id in(" + Master.idStr + ")";
                //if (IsDelivery != "") sql += " and id in (select OrdersID from V_Orders_Goods where IsDelivery=" + IsDelivery + ")";
                List<Model.V_Orders> listModelV_Orders = bllV_Orders.GetModelList(sql);
                rp_Orders.DataSource = listModelV_Orders;
                rp_Orders.DataBind();

            }
        }

        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {


        }


        protected void rp_Orders_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.V_Orders rowData = (Model.V_Orders)e.Item.DataItem;
                //
                OrderState = (EnumHelper.OrdersState)rowData.State;
                BeforeAndBuying = OrderState <= EnumHelper.OrdersState.Buying;
                AfterAndBuying = OrderState >= EnumHelper.OrdersState.Buying;
                //
                Repeater rp_Goods = (Repeater)e.Item.FindControl("rp_Goods");
                ExpectPriceSum = 0; ActualPriceSum_Buying = 0;
                string sql_Orders_Goods = "OrdersID=" + rowData.id + " and ActualAmount>0 ";
                if (IsDelivery != "") sql_Orders_Goods += " and IsDelivery=" + IsDelivery;
                sql_Orders_Goods += "  order by IsDelivery desc,SupplierID, HasBuy";
                listModelV_Orders_Goods = bllV_Orders_Goods.GetModelList(sql_Orders_Goods);
                rp_Goods.DataSource = listModelV_Orders_Goods;
                prevSupplierID = 0;
                rp_Goods.DataBind();
                //
                if (OrderState < EnumHelper.OrdersState.BuyFinish)//采购完成前看不到 实际
                {
                    rp_Goods.Controls[0].FindControl("th_Actual").Visible = false;
                    rp_Goods.Controls[0].FindControl("th_ActualPrice").Visible = false;
                    rp_Goods.Controls[0].FindControl("th_ActualAmount").Visible = false;
                    rp_Goods.Controls[0].FindControl("th_ActualPriceSum").Visible = false;
                    rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("td_Orders_ActualPrice").Visible = false;
                }
                if (OrderState > EnumHelper.OrdersState.Buying)//采购后看不到 预计
                {
                    //rp_Goods.Controls[0].FindControl("th_ExpectPrice").Visible = false;
                    //rp_Goods.Controls[0].FindControl("th_ExpectAmount").Visible = false;
                    //rp_Goods.Controls[0].FindControl("th_ExpectPriceSum").Visible = false;
                    //rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("td_Orders_ExpcetPriceSum").Visible = false;
                }
                //
                ////if (OrderState <= EnumHelper.OrdersState.Buying)//采购之前和采购时 能看到 预计合计
                //((Label)rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("lb_ExpectPay")).Text = StringPlus.showMoney(rowData.ExpectPay);
                if (OrderState >= EnumHelper.OrdersState.Buying)//采购时和采购之后 能看到 实际合计
                {
                    decimal ActualPriceSum = rowData.ActualPriceSum;//
                    if (OrderState == EnumHelper.OrdersState.Buying) ActualPriceSum = ActualPriceSum_Buying;
                    ((Label)rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("lb_ActualPay")).Text = StringPlus.showMoney(rowData.ActualPay);
                }
            }

        }
        protected void rp_Goods_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.V_Orders_Goods rowData = (Model.V_Orders_Goods)e.Item.DataItem;
                //
                if (prevSupplierID == DataTypePlus.requestInt(rowData.SupplierID)) e.Item.FindControl("tr_SupplierName").Visible = false;
                prevSupplierID = DataTypePlus.requestInt(rowData.SupplierID);
                //TextBox tb_ExpectAmount = (TextBox)e.Item.FindControl("tb_ExpectAmount");
                //tb_ExpectAmount.Text = rowData.ExpectAmount.ToString();
                //((TextBox)e.Item.FindControl("tb_id")).Text = rowData.id.ToString();
                //
                //ExpectPriceSum += rowData.ExpectBuyPriceSum;
                // ActualPriceSum_Buying += rowData.ActualBuyPriceSum;
                //
                //小计
                decimal thisGoods_ActualBuyPriceSum = rowData.IsLatePay ? 0 : rowData.HasBuy ? rowData.ActualBuyPriceSum : BBPA.BLL.Goods.GetBuyPriceSum(rowData.GoodsID, rowData.ActualAmount);
                //((Label)e.Item.FindControl("lb_Goods_ActualBuyPriceSum")).Text = StringPlus.showCoin(thisGoods_ActualBuyPriceSum);
                thisSupplier_ExpectBuyPriceSum += rowData.ExpectBuyPriceSum;
                thisSupplier_ActualBuyPriceSum += thisGoods_ActualBuyPriceSum;
                if (e.Item.ItemIndex == listModelV_Orders_Goods.Count - 1 || (e.Item.ItemIndex < listModelV_Orders_Goods.Count - 1 && DataTypePlus.requestInt(rowData.SupplierID) != DataTypePlus.requestInt(listModelV_Orders_Goods[e.Item.ItemIndex + 1].SupplierID)))
                {
                    ((Label)e.Item.FindControl("lb_Supplier_ExpectBuyPriceSum")).Text = StringPlus.showCoin(thisSupplier_ExpectBuyPriceSum);
                    ((Label)e.Item.FindControl("lb_Supplier_ActualBuyPriceSum")).Text = StringPlus.showCoin(thisSupplier_ActualBuyPriceSum);
                    thisSupplier_ExpectBuyPriceSum = thisSupplier_ActualBuyPriceSum = 0;
                }
                else e.Item.FindControl("tr_xiaoji").Visible = false;

                //
                if (OrderState < EnumHelper.OrdersState.BuyFinish)
                {
                    e.Item.FindControl("td_ActualPrice").Visible = false;
                    e.Item.FindControl("td_ActualAmount").Visible = false;
                    e.Item.FindControl("td_ActualPriceSum").Visible = false;
                    e.Item.FindControl("td_Supplier_ExpectBuyPriceSum").Visible = false;
                }
                if (OrderState > EnumHelper.OrdersState.Buying)
                {
                    //e.Item.FindControl("td_ExpectPrice").Visible = false;
                    //e.Item.FindControl("td_ExpectAmount").Visible = false;
                    //e.Item.FindControl("td_ExpectPriceSum").Visible = false;
                }
            }

        }
    }
}